package com.bms.controller;

import com.alibaba.fastjson.JSON;
import com.bms.Dao.UserDao;
import com.bms.bean.User;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.jws.soap.SOAPBinding;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class ReaderManagement {

    @Autowired
    UserDao userDao;

    //获得所有读者信息
    @RequestMapping("/getReader")
    public String getReader() {
        User[] users = userDao.getReader();
        return JSON.toJSONString(users);
    }

    //添加读者
    @RequestMapping("/addReader")
    public String addReader(User user) {
        /*User user = new User();
        user.setUsername("123");
        user.setPassword("456");
        user.setLevel("test");
        user.setContact("123321");
        user.setName("te");*/
        Date date = new Date();
        //设置要获取到什么样的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        //获取String类型的时间
        String number = sdf.format(date);
        String roles = "reader";
        userDao.addReader(user.getUsername(),
                user.getPassword(),
                user.getName(),
                roles,
                number,
                user.getContact(),
                user.getLevel()
        );
        return "ok";
    }

    //根据读者证编号对读者信息修改
    @RequestMapping("/updateReaderByNumber")
    public String updateReaderByNumber(
            String username,
            String password,
            String name,
            String number,
            String contact,
            String level) {
        userDao.updateReader(number,
                username,
                password,
                name,
                contact,
                level
        );
        return "ok";
    }

    //读者证状态修改
    @RequestMapping("updateReaderStateByNumber")
    public String updateReaderStateByNumber(//String number,int state
    ) {
        String number = "1000001";
        int state = 1;
        userDao.updateReaderState(number, state);
        return "ok";
    }
}
